<style lang="less">
@import "./login.less";
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="md-log-in" />
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName"  placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="md-person" />
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password"  password  placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="16" type="md-lock" />
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                    <p class="login-tip">欢迎使用车圈后台管理系统</p>
                </div>
            </Card>
        </div>
        <Spin size="large" fix v-if="loading"></Spin>
    </div>
</template>

<script>
import Cookies from 'js-cookie'
import Main from '../views/Main.vue'
import ajax from '../libs/axios.js'
import { ensure } from '../router/ensure.js'
export default {
    data () {
        return {
            loading: false,
            form: {
                userName: '',
                password: ''
            },
            loginUser: {
                id: '',
                username: '',
                tel: '',
                nickName: '',
                avatar: '',
                userType: '',
                status: '',
                password: ''
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
            }
        }
    },
    computed: {
	    menuList () {
            return this.$store.state.app.menuList
        }
    },
    methods: {
        init () {
	      window.localStorage.clear()
	      this.menuList.length = 0
	  },
        handleSubmit () {
            let vm = this

            this.$refs.loginForm.validate(valid => {
                if (valid) {
                    let data = {
                        tel: vm.form.userName,
                        password: vm.form.password
                    }
                    let hurl = 'adminlogin/suser/weblogin'
                    ajax
                        .formRes(hurl, data)
                        .then(resp => {
                            if (resp.status === '1002') {

                                this.$store.commit('setAvator', resp.loginUser.avatar)
                                let purviewUrlList = resp.functions
                                var otherRouter = []
                                var appRouter = []
                                var comChildObj = {}
                                if (purviewUrlList.length === 0) {
                                    this.$Message.error('该用户没有任何权限，不能登录。')
                                } else {
                                    vm.loading = true
                                    // let menuFlag = purviewUrlList.length === 0 ? 'isNull' : 'notNull'
                                    // window.localStorage.setItem('menuFlag', menuFlag)
                                    purviewUrlList.forEach(item => {
                                        // 权限设置第一层
                                        if (item.parentid === 0) {
                                            var faList = {
                                                path: '/' + item.functionpath,
                                                name: item.functionname,
                                                title: item.functiontitle,
                                                component: Main,
                                                icon: item.icon,
                                                children: []
                                            }
                                        }

                                        item.seconds.forEach(index => {
                                            ensure.forEach(com => {
                                                comChildObj[com.name] = com
                                            })

                                            if (index.functiontype === 1) { // 菜单
                                                let showChildList = {
                                                    path: index.functionpath,
                                                    name: index.functionname,
                                                    title: index.functiontitle,
                                                    icon: index.icon,
                                                    component: comChildObj[index.functionname]
                                                }
                                                faList.children.push(showChildList)
                                            } else if (index.functiontype === 2) { // 按钮
                                                console.log(index, '=====')
                                                var hideChildList = {
                                                    path: index.functionpath + '/:id',
                                                    title: index.functiontitle,
                                                    name: index.functionname,
                                                    component: comChildObj[index.functionname]
                                                }
                                                otherRouter.push(hideChildList)
                                            }
                                        })
                                        appRouter.push(faList)
                                    })
                                    this.$router.addRoutes(appRouter, otherRouter)
                                    Cookies.set('user', resp.loginUser.username)
                                    Cookies.set('password', resp.loginUser.password)
                                    Cookies.set('admin-login', resp.loginUser.id)
                                    window.localStorage.setItem('otherRouter', JSON.stringify(otherRouter))
                                    window.localStorage.setItem('appRouter', JSON.stringify(appRouter))
                                    window.localStorage.setItem('userId', resp.loginUser.id)
                                    window.localStorage.setItem('userName', resp.loginUser.username)
                                    window.localStorage.setItem('userTel', resp.loginUser.tel)
                                    window.localStorage.setItem('loginDate', new Date())
                                    window.localStorage.setItem('avatorImgPath', resp.loginUser.avatar)

                                    vm.loginUser = resp.loginUser
                                    this.$router.push({
                                        name: 'home_index'
                                    })
                                }

                            } else if (resp.status === '1000') {
                                this.$Message.error('用户不存在')
                            } else if (resp.status === '1001') {
                                this.$Message.error('密码错误')
                            } else {
                                this.$Message.error('登录失败')
                            }
                            vm.loading = false
                        })
                        .catch(resp => {
                            this.$Message.error('登录失败，请您稍后再试')
                            this.loading = false
                        })

                    /*  if (this.form.userName === "iview_admin") {
            Cookies.set("access", 0);
          } else {
            Cookies.set("access", 1);
          } */
                }
            })
        }
    },
    mounted: function () {
	    this.init()
    }
}
</script>

<style>
</style>
